﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Fin;
using nERP.Model;
using nERP.Model.Bas;

namespace nERP.ViewModel.Fin.OtherIncomeVMs
{
    public partial class OtherIncomeListVM : BasePagedListVM<OtherIncome_View, OtherIncomeSearcher>
    {

        protected override IEnumerable<IGridColumn<OtherIncome_View>> InitGridHeader()
        {
            return new List<GridColumn<OtherIncome_View>>{

                this.MakeGridHeader(x => x.OtherIncome_OtherIncomeCode).SetTitle(@Localizer["Page.其他收入单号"].Value),
                this.MakeGridHeader(x => x.OtherIncome_Customer).SetTitle(@Localizer["Page.对方单位"].Value),
                this.MakeGridHeader(x => x.OtherIncome_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.OtherIncome_InvoicesDate).SetTitle(@Localizer["Page.单据日期"].Value),
                this.MakeGridHeader(x => x.OtherIncome_SettlementAccount).SetTitle(@Localizer["Page.结算账户"].Value),
                this.MakeGridHeader(x => x.OtherIncome_IncomeType).SetTitle(@Localizer["Page.收入类别"].Value),
                this.MakeGridHeader(x => x.OtherIncome_TotalPrice).SetTitle(@Localizer["Page.金额"].Value),
                this.MakeGridHeader(x => x.OtherIncome_Status).SetTitle(@Localizer["Page.单据状态"].Value),
                this.MakeGridHeader(x => x.OtherIncome_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.OtherIncome_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.OtherIncome_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.OtherIncome_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.OtherIncome_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.OtherIncome_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),


                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<OtherIncome_View> GetSearchQuery()
        {
            var query = DC.Set<OtherIncome>()

                .CheckContain(Searcher.OtherIncomeCode, x => x.OtherIncomeCode)
                .CheckEqual(Searcher.CustomerId, x => x.CustomerId)
                .CheckEqual(Searcher.MakeUserId, x => x.MakeUserId)
                .CheckBetween(Searcher.InvoicesDate?.GetStartTime(), Searcher.InvoicesDate?.GetEndTime(), x => x.InvoicesDate, includeMax: false)
                .CheckEqual(Searcher.SettlementAccountId, x => x.SettlementAccountId)
                .CheckEqual(Searcher.IncomeTypeId, x => x.IncomeTypeId)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new OtherIncome_View
                {
                    ID = x.ID,

                    OtherIncome_OtherIncomeCode = x.OtherIncomeCode,
                    OtherIncome_Customer = DC.Set<Customer>().Where(z0 => z0.CustomerCode == x.CustomerId).Select(y => y.CustomerName).FirstOrDefault(),
                    OtherIncome_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    OtherIncome_InvoicesDate = x.InvoicesDate,
                    OtherIncome_SettlementAccount = DC.Set<SettlementAccount>().Where(z0 => z0.Code == x.SettlementAccountId).Select(y => y.Name).FirstOrDefault(),
                    OtherIncome_IncomeType = x.IncomeType.DicFieldDes,
                    OtherIncome_TotalPrice = x.TotalPrice,
                    OtherIncome_Status = x.Status,
                    OtherIncome_Remark = x.Remark,
                    OtherIncome_CreateTime = x.CreateTime,
                    OtherIncome_UpdateTime = x.UpdateTime,
                    OtherIncome_CreateBy = x.CreateBy,
                    OtherIncome_UpdateBy = x.UpdateBy,
                    OtherIncome_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }


        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                var ids = GetMyApproves();
                foreach (var item in EntityList)
                {
                    var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                    if (m != null)
                    {
                        item.wtmtemp_CanApprove = true;
                    }
                }
            }
        }


    }
    public class OtherIncome_View : OtherIncome
    {

        public string OtherIncome_OtherIncomeCode { get; set; }
        public string OtherIncome_Customer { get; set; }
        public string OtherIncome_MakeUser { get; set; }
        public DateTime? OtherIncome_InvoicesDate { get; set; }
        public string OtherIncome_SettlementAccount { get; set; }
        public string OtherIncome_IncomeType { get; set; }
        public decimal? OtherIncome_TotalPrice { get; set; }
        public StatusTypeEnum? OtherIncome_Status { get; set; }
        public string OtherIncome_Remark { get; set; }
        public DateTime? OtherIncome_CreateTime { get; set; }
        public DateTime? OtherIncome_UpdateTime { get; set; }
        public string OtherIncome_CreateBy { get; set; }
        public string OtherIncome_UpdateBy { get; set; }
        public bool OtherIncome_IsValid { get; set; }
        public bool wtmtemp_CanApprove { get; set; }

    }

}